#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int a, b, q, l, r;
vector<int> v;
int gcd(int a, int b)
{
    return b ? gcd(b, a % b) : a;
}
int main()
{
    cin >> a >> b;
    cin >> q;
    int d = gcd(a, b);
    for (int i = 1; i <= d / i; i++)
    {
        if (d % i == 0)
        {
            v.push_back(i);
            if (d / i != i) v.push_back(d / i);
        }
    }
    sort(v.begin(), v.end());
    while (q--)
    {
        cin >> l >> r;
        int ll = 0, rr = v.size() - 1;
        while (ll < rr)
        {
            int mid = ll + rr + 1 >> 1;
            if (v[mid] > r) rr = mid - 1;
            else ll = mid;
        }
        //cout<<v[ll]<<endl;
        if (v[ll]<l || v[ll]>r) cout << -1 << endl;
        else cout << v[ll] << endl;
    }
    return 0;
}